package com.yhy.tradesystem.datasource;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Random;

import javax.xml.transform.stream.StreamResult;

import com.yhy.tradesystem.util.Log;

public class YahooFileDataSource {
	
	final static int DATE_INDEX=0;
	final static int OPEN_INDEX=1;
	final static int HIGH_INDEX=2;
	final static int LOW_INDEX=3;
	final static int CLOSE_INDEX=4;
	final static int VOL_INDEX=5;
	
	public final static String TAG="yhy";
	
	public ArrayList<Float> rawDataClose=new ArrayList<Float>();
	public ArrayList<Float> rawDataOpen=new ArrayList<Float>();
	public ArrayList<Float> rawDataHigh=new ArrayList<Float>();
	public ArrayList<Float> rawDataLow=new ArrayList<Float>();
	public ArrayList<Date> rawDataDate=new ArrayList<Date>();
	public ArrayList<Long> rawDataVol=new ArrayList<Long>();
	
	protected FileReader freader = null;
	protected BufferedReader bfreader = null;
	private int totalDataSizeInBytes;
	
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	
	public boolean init(String rootDir,String name) {
		try {
			FileInputStream fis = new FileInputStream(rootDir+"/" +name+".csv");
			totalDataSizeInBytes = fis.available();
			fis.close();
			
			freader = new FileReader(rootDir+"/" +name+".csv");
			bfreader = new BufferedReader(freader);
			bfreader.readLine();// skip 1st line
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean seekToRandomLinePosition(){
		Random rand=new Random();
		int skip = rand.nextInt( totalDataSizeInBytes/4 * 4/5);//不能skip太多，至少要留下若干条数据作为历史记录充满buffer，另外最好留下一年的数据
		try {
			bfreader.skip(skip);//use filereaer to skip will cause bfreader goes wrong!
			String line = bfreader.readLine();
			Log.d(TAG, String.format("skip %d in %d line %s", skip,totalDataSizeInBytes,line));
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean release(){
		try {
			bfreader.close();
			freader.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public void loadData(){
		long cnt=0;
		try {
			String line = bfreader.readLine();
			while(line!=null){
				String[] raw = line.split(",");
				if(raw.length!=7){
					Log.d("yhy", "loadData "+raw.length+" wrong data "+line);
					break;
				}
				rawDataClose.add(Float.parseFloat(raw[CLOSE_INDEX]));
				rawDataOpen.add(Float.parseFloat(raw[OPEN_INDEX]));
				rawDataHigh.add(Float.parseFloat(raw[HIGH_INDEX]));
				rawDataLow.add(Float.parseFloat(raw[LOW_INDEX]));
				rawDataVol.add(Long.parseLong(raw[VOL_INDEX]));
				try {
					rawDataDate.add(formatter.parse(raw[DATE_INDEX]));
				} catch (ParseException e) {
					e.printStackTrace();
					Log.d(TAG, "ParseException "+raw[DATE_INDEX]);
				}
				line = bfreader.readLine();
				cnt++;
				if(cnt % 100 ==0){
					Log.d("yhy", "loadData "+cnt);
				}
			}
			//yahoo数据是从新到旧，需要反转
			Collections.reverse(rawDataClose);
			Collections.reverse(rawDataOpen);
			Collections.reverse(rawDataHigh);
			Collections.reverse(rawDataLow);
			Collections.reverse(rawDataVol);
			Collections.reverse(rawDataDate);
			
			Log.d("yhy", "loadData ok");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
}
